package com.citasgoo.aliyunFacility.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.citasgoo.aliyunFacility.api.AliyunKey;
import com.citasgoo.aliyunFacility.api.SmsEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

public class AliyunOssUtil {

    private final static Logger logger = LoggerFactory.getLogger(AliyunOssUtil.class);

    // Endpoint以杭州为例，其它Region请按实际情况填写。
    public static final String END_POINT = "http://oss-cn-hangzhou.aliyuncs.com";

    private static final String SMS_CODE = "SMS_CODE";

    /**
     * 流式上传：上传文件流
     * @param yourBucketName
     * @param yourObjectName
     * @param inputStream
     * @return
     * @throws FileNotFoundException
     */
    private static void uploadInputStream(String yourBucketName, String yourObjectName, InputStream inputStream ){
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(END_POINT, AliyunKey.getAccessKeyId(), AliyunKey.getAccessSecret());
        // 上传文件流。
        ossClient.putObject(yourBucketName, yourObjectName, inputStream);
        // 关闭OSSClient。
        ossClient.shutdown();
    }


    /**
     *
     * @param catalog 文件名，根目录默认为"/"，最后要以"/"结尾
     * @param fileName
     * @param inputStream
     * @return
     */
    public static String uploadFileInputStream(String catalog, String fileName, InputStream inputStream){
        SimpleDateFormat formater = new SimpleDateFormat("yyyyMMddHHmmss");
        fileName = formater.format(new Date()) + "-" + fileName;
        uploadInputStream(AliyunKey.getBucketName(),catalog + fileName, inputStream);
        return fileName;
    }


    /**
     *
     * @param catalog 文件名，根目录默认为"/"，最后要以"/"结尾
     * @param file
     */
    private static String uploadFile( String catalog, File file ){
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(END_POINT, AliyunKey.getAccessKeyId(), AliyunKey.getAccessSecret());
        String fileName = catalog + file.getName();
        // 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest(AliyunKey.getBucketName(), fileName, file);
        // 如果需要上传时设置存储类型与访问权限，请参考以下示例代码。
        // ObjectMetadata metadata = new ObjectMetadata();
        // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
        // metadata.setObjectAcl(CannedAccessControlList.Private);
        // putObjectRequest.setMetadata(metadata);
        // 上传文件。
        ossClient.putObject(putObjectRequest);
        // 关闭OSSClient。
        ossClient.shutdown();
        return fileName;
    }

    private static void downloadFile(String catalogName,String localFile){
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(END_POINT, AliyunKey.getAccessKeyId(), AliyunKey.getAccessSecret());
        // 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖，不存在则新建。
        ossClient.getObject(new GetObjectRequest(AliyunKey.getBucketName(), catalogName), new File(localFile));
        // 关闭OSSClient。
        ossClient.shutdown();
    }
}
